package pv.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;

public class NetworkHelper {
    public static final int CONNECTION_TIMEOUT = 15000;
    public static final int SOCKET_TIMEOUT = 15000;
    public static final int READTIMEOUT = 10000;

    public static HttpClient createHttpClient() {
        // TODO Auto-generated method stub
        DefaultHttpClient client;

        client = new DefaultHttpClient();

        HttpParams params = client.getParams();
        HttpConnectionParams.setConnectionTimeout(params,
                NetworkHelper.CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, NetworkHelper.SOCKET_TIMEOUT);
        client = new DefaultHttpClient(params);

        return client;
    }

    public static HttpURLConnection createHttpURLConnection(String strUrl)
            throws IOException {
        // TODO Auto-generated method stub
        URL url = new URL(strUrl);
        HttpURLConnection connect = (HttpURLConnection) url.openConnection();
        connect.setReadTimeout(NetworkHelper.READTIMEOUT /* milliseconds */);
        connect.setConnectTimeout(NetworkHelper.CONNECTION_TIMEOUT /* milliseconds */);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
            System.setProperty("http.keepAlive", "false");

        }
        return connect;
    }

    public boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        // if no network is available networkInfo will be null
        // otherwise check if we are connected
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }

    public static String convertToString(HttpResponse response)
            throws ParseException, IOException {

        return EntityUtils.toString(response.getEntity());
    }

    public String readIt(InputStream stream, int len) throws IOException,
            UnsupportedEncodingException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        char[] buffer = new char[len];
        reader.read(buffer);

        return new String(buffer);
    }

    public static String encodeURL(String url)
            throws UnsupportedEncodingException {
        return URLEncoder.encode(url, "UTF-8");
    }

}
